# -*- coding: utf-8 -*-
"""
@Env 
@Time 2024/9/4 上午11:18
@Author yzpang
@Function:
    接收api接口传递的参数,
    从注册中心获取在线模型信息,
    使用队列发送推理文本到模型子进程,
    接收推理结果并返回
"""
from ..entity.request import InferenceMessage
from ..config.registry import model_registry
from modelserver.configs.base_config import get_logger

logger = get_logger()


def inference_text(infer_info: InferenceMessage):
    """文本推理服务"""
    model_name = infer_info.modelName
    text = infer_info.text
    # 判断模型是否在线
    if model_name not in model_registry:
        msg = f"模型{model_name}不存在"
        logger.info(msg)
        raise KeyError(msg)
    process_info = model_registry[model_name]
    # 向模型发送推理文本,接收推理结果
    inbox = process_info.inbox
    outbox = process_info.outbox
    inbox.put("INFER " + text)
    result = outbox.get()
    print(f'inference result: {result}')
    return result

